/*
 * 创建日期 2009-3-17
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.grid.tag;

import  org.fantasy.common.grid.bean.ReportTagParam;

import javax.servlet.jsp.JspException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *  AbstractImsTag
 * @author: 王文成
 * @version: 1.0
 * @since 2009-3-17
 */
public abstract class AbstractReportTag extends AbstractTag {
    
    private final Log log = LogFactory.getLog(AbstractReportTag.class);

    public static final String REPORT_PARAM = "REPORT_PARAM";

    public AbstractReportTag(){
        log.debug(">>>>>>>>>>>>>>>>>>> New AbstractReportTag instance!");
    }
    
    @Override
    // 每次调用标签前初始化
    public void init() {
        pageContext.setAttribute(REPORT_PARAM,new ReportTagParam());
    }

    public void setVar(String var) {
        getReportParam().setVar(var);
    }
    
    public void setData(String data) {
        getReportParam().setData(data);
    }

    public void setTableConfig(String tableConfig) {
        getReportParam().setTableConfig(tableConfig);
    }

    @Override
    public int doStartTag() throws JspException {
        // 初始化参数
        getReportParam().setReportType(getReportType());
        getReportParam().setHttpServletRequest(getRequest());
        return EVAL_BODY_INCLUDE;
    }
    
    /**
     * 取得报表类型
     * @return
     */
    protected abstract String getReportType();
    
    @Override
    public int doAfterBody() throws JspException {
        try {
            // 初始化参数
            getReportParam().prepare();
        } catch (Exception e) {
            log.error(e);
            e.printStackTrace();
            throw new JspException(e);
        }
        return EVAL_PAGE;
    }
    
    /**
     * 标签结束
     * 
     * @return
     * @throws JspException
     */
    @Override
    public int doEndTag() throws JspException {
        try {
            getWriter().write(getReportHtml());
        } catch (Exception e) {
            log.error(e);
            e.printStackTrace();
            throw new JspException(e);
        }
        return EVAL_PAGE;
    }
    
    public ReportTagParam getReportParam(){
        return (ReportTagParam)pageContext.getAttribute(REPORT_PARAM);
    }
    
    /**
     * 取得报表内容
     * @return
     * @throws Exception
     */
    protected abstract String getReportHtml() throws Exception;
}
